"use strict";

import {
    //核心
    React, Enhance, Controller, $,
    tools,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Panel,
    Input, BoxWidget, Button, Lock, Directive, Http
} from 'admin';

var {http} = tools;
import valid from '../../validate/admin.json';
var lifts = [];
var num = [-2, -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
for (var i = 0; i < num.length; i++) {
    lifts.push({title: num[i], value: num[i]});
}

class SourceClientController extends Controller {
    state = {
        tableProps: null,
        sex: [
            {title: '女', value: 0},
            {title: '男', value: 1}
        ],
        edit: {}
    }

    constructor() {
        super();
        this.state.tableProps = {
            update: {},
            query: {},
            types: {
                state: function (data, options) {
                    // debugger;
                    if (data.val == 1)
                        return <span className='label bg-red'>男</span>;
                    if (data.val == 0)
                        return <span className='label bg-green'>女</span>;
                }
            },
            buttons: {
                edit: {
                    onClick: this._handlerEditDialog
                },
                // upload: this._handlerCreateUpload,
                'reset':{
                    icon:{icon:'writefill'},
                    onClick: (data) => {
                        this._emit('Confirm', {
                            title: `是否重置密码为：000000?`, onConfirm: () => {
                                //console.log(1)
                                this.refs.http2.send(this._action('do_reset'), {id: data.id,user_name: data.user_name})
                                    .success((res) => {
                                        if (res) {
                                            window.toast(`重置成功`);
                                            // this.refs.table2.remove(data.id);
                                        }
                                    })
                            }
                        })
                    }

                },
                'delete': {
                    onClick: this._handlerDeleteDialog
                }
            },
            pk: 'id',
            fields: [
                {
                    order: 0,
                    name: 'id',
                    width: 50,
                },
                {
                    order: 0,
                    name: 'img',
                    type: {
                        name: 'image',
                        options: {
                            baseUrl: window.RESOURCE_URL + 'header/'
                        }
                    },
                },
                {
                    order: 0,
                    name: 'user_name',
                },
                {
                    order: 0,
                    name: 'name',
                },
                {
                    order: 0,
                    name: 'account',
                },
                {
                    order: 0,
                    name: 'sex',
                    type: {
                        name: 'state'
                    }
                },
                {
                    order: 0,
                    name: 'address',
                },
                {
                    order: 0,
                    name: 'lift',
                },
                {
                    order: 0,
                    name: 'time',
                    type: {
                        name: 'datetime'
                    }
                },

            ]
        };
    }

    render() {
        var {edit} = this.state;
        return (
            <Grid>
                <Grid col="12 12 12 12">
                    <BoxWidget
                        elem={Form}
                        onSubmit={this._handlerSearch.bind(this)}
                        color="4" buttons={[{icon:'search',title:'search',type:'submit'}]}
                        className="no-padding"
                    >
                        <Grid col="2 2 2 2"><Input type="text" name="account" message={this._lang('account')}/></Grid>
                    </BoxWidget>
                    <BoxWidget color="3" buttons={[{icon:'add',title:'add',onClick:this._handlerAddDialog}]}>
                        <Lock>
                            <Http ref="http2"></Http>
                            <Table ref="table" {...this.props} {...this.state.tableProps}></Table>
                        </Lock>
                    </BoxWidget>
                </Grid>
                {/*添加*/}
                <Directive name="Modal">
                    <Dialog
                        ref="addDialog"
                        elem={Form}
                        {...this.props}
                        action="do_add"
                        title={this._lang("add")}
                        onSuccess={this._handlerAddSuccess}
                        rules={valid[this.props.controller].do_add}
                        buttons={[{title:'submit',type:'submit'}]}
                    >
                        <Input type="text" name="user_name" message={this._lang('user_name')}/>
                        <Input type="password" name="pwd" message={this._lang('pwd')}/>
                        <Input type="text" name="name" message={this._lang('name')}/>
                        <Input type="text" name="account" message={this._lang('account')}/>
                        <Input type="select" name="sex" value="" option={this.state.sex}
                               message={this._lang("sex")}></Input>
                        <Input type="text" name="address" message={this._lang('address')}/>
                        <Input type="checkgroup" name="lift" message={this._lang('lift')} option={lifts}/>
                    </Dialog>
                </Directive>
                {/*编辑*/}
                <Directive name="Modal">
                    <Dialog
                        ref="editDialog"
                        elem={Form}
                        {...this.props}
                        action="do_edit"
                        title={this._lang("edit")}
                        onSuccess={this._handlerEditSuccess}
                        rules={valid[this.props.controller].do_edit}
                        buttons={[{title:'submit',type:'submit'}]}
                    >
                        <Input type="hidden" name="id" value={edit.id}></Input>
                        <Input type="text" name="user_name" message={this._lang('user_name')} value={edit.user_name} readOnly={true}/>
                        {/*<Input type="password" name="pwd" message={this._lang('pwd')}/>*/}
                        {/*<Input type="password" name="password_check" message={this._lang('password_check')}/>*/}
                        <Input type="text" name="name" message={this._lang('name')} value={edit.name}/>
                        <Input type="text" name="account" message={this._lang('account')} value={edit.account} readOnly={true}/>
                        <Input type="select" name="sex" option={this.state.sex}
                               message={this._lang("sex")} value={edit.sex}></Input>
                        <Input type="text" name="address" message={this._lang('address')} value={edit.address}/>
                        <Input type="checkgroup" name="lift" message={this._lang('lift')} option={lifts} value={edit.lift}/>
                    </Dialog>
                </Directive>

                <Directive name="Modal">
                    <Dialog
                        ref="deleteDialog"
                        elem={Form}
                        {...this.props}
                        action="do_delete"
                        title={this._lang('delete')}
                        rules={valid[this.props.controller].do_delete}
                        buttons={[{title:'submit',type:'submit'}]}
                        onSuccess={this._handlerDeleteSuccess.bind(this)}
                    >
                        <input type="hidden" name="id" value={edit.id}/>
                    </Dialog>
                </Directive>
            </Grid>
        );
    }

    /**
     *
     * @private
     */
    _handlerAddDialog() {
        this.refs.addDialog.show();
    }

    /**
     *
     * @param edit
     * @private
     */
    _handlerEditDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.editDialog.show();
        }).bind(this));
    }

    _handlerEditSuccess(data) {
        if (data) {
            this.refs.table.update(data);
            this.refs.editDialog.hide();
        }
    }

    /**
     *
     * @private
     */
    _handlerAddSuccess(data) {
        if (data) {
            this.refs.table.refresh();
            this.refs.addDialog.hide();
        }
    }

    /**
     *
     * @param key
     * @param data
     * @param index
     * @returns {XML}
     * @private
     */
    _handlerCreateUpload(key, data, index) {
        return (
            <Upload
                url={window.SERVER_URL+"/scdc/upload/get_header"}
                onSuccess={this._handlerUploadSuccess}
                post={{id:data.id}}
            ><Icon icon="upload" type="admin"></Icon>
            </Upload>
        );
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerUploadSuccess(data) {
        this.refs.table.update(data);
    }

    /**
     *
     * @param data
     * @returns {boolean}
     * @private
     */
    _handlerSearch(data) {
        this.refs.table.query(data);
        return false;
    }

    /**
     *
     * @param delete
     * @private
     */
    _handlerDeleteDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.deleteDialog.show();
        }).bind(this));
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerDeleteSuccess(data) {
        if (data) {
            this.refs.table.refresh(data);
            this.refs.deleteDialog.hide();
            window.toast(<Lang lang="message.success.delete"/>);
        } else {
            window.toast(<Lang lang="message.error.delete"/>);
        }
    }
}

export default Enhance(SourceClientController);